<?php
/**
 * @author speakphp.com
 * @新闻类别
 */
class NewstypeAction extends BaseAction{
	private $category_modeldata_cache; 
	private $category_ ; 
	public $d =  '' ;
	function __construct(){
		parent::__construct();
		$this->d = D("Admin/News");
	}
	function index(){
		$action = $this->_request("action");	
		$action_array = array("show","ajax_data",'upload_pic');
		$action = !in_array($action,$action_array)?'show':$action ;
		if($action == 'show'){
			$this->display();
		}elseif($action == 'ajax_data'){
			$this->ajax_data();
		}elseif($action == 'upload_pic'){
			$this->upload_pic();
		}		
	}
	//ajax get data
	private function ajax_data(){
		$page = intval($this->_request("page"));
		$list = $this->d->queryNewsTypeList($page , 500 );
		echo result_to_towf_new($list['list'], 1, '成功', $list['page_string']) ;
	}
	//添加新闻分类
	function add(){		
		$action = $this->_request("action");		
		$action_array = array("add","doadd");
		$action = !in_array($action,$action_array)?'add':$action ;	
		if($action == 'add'){	
			$pid = intval($this->_request("pid")) ;
			$typename =( $this->d->getTypeName($pid) == '' )?"顶层栏目": $this->d->getTypeName($pid);			
			$this->assign("pid" ,$pid);
			$this->assign("typename",$typename);
			$this->display();		
		}elseif($action =='doadd'){
			$this->doadd();
		}
	}
	//处理添加
	private function doadd(){	
		$res = $this->d->insertNewType();
		if($res['status']){
			echo result_to_towf_new('', 1, $res['message'], null);
			die();
		}else{
			echo result_to_towf_new('', 0,$res['message'], null);
			die() ;
		}
	}
	//编辑页面
	function edit(){
		$action = $this->_request("action" , true );		
		$action_array = array("edit","doedit");
		$action = !in_array($action,$action_array)?'edit':$action ;		
		if($action == 'edit'){
			$result = $this->d->make_option_data();//新闻类别
			$id = intval($this->_request("id"));//数据			
			$info_ = $this->d->getNewsTypeData($id);
			if(empty($info_)){
				throw_exception("暂无数据");
			}				
			$this->assign("info" , $info_);		
			$this->assign("id" , $id );
			$this->assign("category" , $result);
			$this->display();	
			
		}elseif($action == 'doedit'){
			$this->doedit();
		}

	}
	//处理编辑数据
	private function doedit(){
		$res = $this->d->editNewsType();
		if($res['status']){
			echo result_to_towf_new('', 1, $res['message'], null);
			die();
		}else{
			echo result_to_towf_new('', 0,$res['message'], null);
			die() ;
		}
	}
	//上传图片
	public function upload_pic(){
		$res = $this->d->upload_pic();
		if($res['status']){
			echo result_to_towf_new(array('pic'=>$res['pic']) , 1, $res['message'], null);
			die();
		}else{
			echo result_to_towf_new($res, 0,$res['message'], null);
			die() ;
		}
	}
		
}
//file end